﻿@using System.Data.Entity

@{
    //เช็คการรับเข้า
    //1. เช็คว่ามี Receive ที่ไม่มี Lot หรือถูกลบ
    //2. เช็คว่ามี Lot ที่ไม่มี Recieve หรือถูกลบ
    var db = new GERPContext();
    var receives = db.Receiveds.Where(o => o.RecordStatus != "D").ToList();
    var lots = db.OfficeSuppliesLots.Include(o=>o.ReceiveDtl.Received)
        .Where(o => o.RecordStatus != "D")
        .OrderBy(o=>o.CreatedWhen)
        .ToList();
    var resItemIdAvails = db.DispenseHistorys.Where(o => o.RecordStatus != "D").GroupBy(o => o.ResItemId)
        .Where(o => o.Key.HasValue).Select(o => o.Key.Value);
    string debugText = "";
    bool error = false;
    @*foreach (var lot in lots)
    {
        if (lot.ReceiveDtl == null || lot.ReceiveDtl.Received == null
            || lot.ReceiveDtl.RecordStatus == "D" || lot.ReceiveDtl.Received.RecordStatus == "D")
        {
            debugText += "Receive is null [lotid=" + lot.OfficeSuppliesLotId + "]\r\n";
            error = true;
        } 
    }

    if (error == true)
    {
        <pre>DebugText = @debugText </pre>
        return;
    }*@
    
    
    ////////////ทำการเติม Quantity
    ////////////1. ปรับ Lot Quantity ทั้งหมดให้มีค่าเท่ากับตอนรับ Receive
    //////////foreach (var m in lots.Where(o=>o.Quantity != o.ReceiveDtl.Quantity))
    //////////{
    //////////    m.Quantity = m.ReceiveDtl.Quantity;
    //////////    db.Entry(m).State = EntityState.Modified;
    //////////    db.SaveChanges(); 
    //////////}
    
    ////////////ทำการยกเลิกเบิก
    ////////////1. ปรับ dispensestatus ในเบิก requestdtl ทั้งหมดให้เป้น false
    //////////foreach (var m in db.RequestParcelDtls.Where(o=>o.DispenseStatus != null).ToList())
    //////////{
    //////////    m.DispenseStatus = null;
    //////////    db.Entry(m).State = EntityState.Modified;
    //////////    db.SaveChanges(); 
    //////////}
    
    
    //ทำการเคลียร์จ่ายทั้งหมด
    /*foreach (var m in db.Dispenses.ToList())
    {
        m.RecordStatus = "D";
        db.Entry(m).State = EntityState.Modified;
        db.SaveChanges(); 
    }
    foreach (var m in db.DispenseHistorys.ToList())
    {
        m.RecordStatus = "D";
        db.Entry(m).State = EntityState.Modified;
        db.SaveChanges(); 
    }*/
    
    var dispenseHises = db.DispenseHistorys.Include(o=>o.Dispense).Where(o=>o.RecordStatus != "D" && o.Dispense.RecordStatus != "D").ToList(); 
        
    var receiveDtls = db.ReceiveDtls.Include(o=>o.Received).Where(o=>o.RecordStatus != "D" && o.Received.RecordStatus != "D").ToList();
    
    var offices = db.OfficeSuppliesLots.Include(o=>o.ReceiveDtl.Received).Where(o=>o.RecordStatus != "D" && o.ReceiveDtl.Received.RecordStatus != "D").ToList();
    
    var resItems = db.MasResourceItems.Where(o => resItemIdAvails.Contains(o.ResItemId)).ToList();
    
    // แสดงการตัดจ่าย
    foreach (var resItemId in resItemIdAvails)
    {
        var r1 = dispenseHises.Where(o => o.ResItemId == resItemId);
        var r2 = offices.Where(o => o.ResItemId == resItemId);

        List<DateTime> d1 = r1.Select(o => o.CreatedWhen.Value).ToList();

        d1.AddRange(r2.Select(o => o.CreatedWhen.Value).ToList());
        
        List<DateTime> d3 = new List<DateTime>();
        foreach (var d in d1)
        {
            if (!d3.Contains(d))
            {
                d3.Add(d);
            }
        }
        d3 = d3.OrderBy(o => o).ToList();
        
        var resItem = resItems.First(o => o.ResItemId == resItemId);
        
        <h4>ResItemId = @resItemId - รหัสพัสดุ = @resItem.Code, ชื่อพัสดุ = @resItem.Name</h4>

        decimal quantity = 0;
        foreach (var d in d3)
        {
            var r2F = r2.Where(o => o.CreatedWhen == d);
            var r1F = r1.Where(o => o.CreatedWhen == d);

            quantity += r2F.Sum(o => o.ReceiveDtl.Quantity);
            quantity -= r1F.Sum(o => o.Quantity??0);
            
            <text>
                @if (quantity < 0)
                {
                    @Html.Raw("<span style=\"background-color: red;\">")
                }
        @d

        @if (r2F.Any())
                {
            <span style="color: green">
                รับ @string.Join(",", r2F.Select(o => o.ReceiveDtl.Received.ReceiveCode + " รับเข้าล็อต " + o.LotNo + " [รับ" + o.Quantity + "อัน]"))
            </span>
            @:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                }
        @if (r1F.Any())
                {
            <span style="color: blue">
                จ่าย @string.Join(",", r1F.Select(o => o.Dispense.Code + " จากล็อต " + o.LotNo + " [จ่าย" + o.Quantity + "อัน]"))
            </span>
            @:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                }
        เหลือ [@quantity อัน]@if (quantity < 0)
                             {
                                @Html.Raw("</span >")
                             }
                    <br />
            </text>
        }
    }

}

<h2>พัสดุที่มี</h2>
<table border="1">
    <tr>
        <td>ประเภท</td>
        <td>หมวด</td>
        <td>กลุ่ม</td>
        <td>รายการ</td>
    </tr>
    @foreach (var res in db.MasResourceItems.Include(o => o.MasResourceSubGroup.MasResourceGroup.MasResourceType).Where(o => o.RecordStatus != "D")
    .OrderBy(o => o.MasResourceSubGroup.MasResourceGroup.MasResourceType.ResTypeId)
    .ThenBy(o => o.MasResourceSubGroup.MasResourceGroup.Code)
    .ThenBy(o => o.MasResourceSubGroup.Code)
    .ThenBy(o => o.Code))
    {
        <tr>
            <td>@Html.DisplayFor(m=>res.MasResourceSubGroup.MasResourceGroup.MasResourceType.Code) - @Html.DisplayFor(m => @res.MasResourceSubGroup.MasResourceGroup.MasResourceType.Name)</td>
            <td>@Html.DisplayFor(m => res.MasResourceSubGroup.MasResourceGroup.Code) - @Html.DisplayFor(m =>@res.MasResourceSubGroup.MasResourceGroup.Name)</td>
            <td>@Html.DisplayFor(m=>res.MasResourceSubGroup.Code) - @Html.DisplayFor(m =>@res.MasResourceSubGroup.Name)</td>
            <td>[@Html.DisplayFor(m => @res.ResItemId)] @Html.DisplayFor(m => @res.Code) - @Html.DisplayFor(m =>@res.Name)</td>
        </tr>
    }
    
    
</table>